#include "xtransaction.h"
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <Objbase.h>
#endif
#include "xrequest.h"
#include "xresponse.h"
#include "xtransactionstate.h"
#include "xcall.h"

XSIPTransaction::XSIPTransaction(int type)
{
	m_transactionType = type;
	m_owner = NULL;
	m_transport = NULL;
	m_request = NULL;
	m_response = NULL;
	memset(m_id, 0, sizeof(m_id));
	m_state = NULL;
	m_ack = NULL;
	m_tsxState = 0;
#ifdef WIN32
	GUID guid;
	CoCreateGuid(&guid);
	sprintf(m_id, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
		guid.Data1
		, guid.Data2
		, guid.Data3
		, guid.Data4[0], guid.Data4[1]
	, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
	, guid.Data4[6], guid.Data4[7]);
#endif
}

XSIPTransaction::~XSIPTransaction()
{
	m_owner = NULL;
	if (m_request)
	{
		delete m_request;
	}
	m_request = NULL;
	if (m_response)
	{
		delete m_response;
	}
	m_response = NULL;
	if (m_state)
		delete m_state;
	m_state = NULL;
	if (m_ack)
		delete m_ack;
	m_ack = NULL;
}

int XSIPTransaction::getTransactionType()
{
	return m_transactionType;
}

void* XSIPTransaction::getObject(const char* name) const
{
	if (strcmp(name, "XSIPTransaction") == 0)
		return const_cast<XSIPTransaction*>(this);
	else
		return XSIPObject::getObject(name);
}

void XSIPTransaction::setTransactionId(char* id)
{
	sprintf(m_id, "%s", id);
}

char* XSIPTransaction::getTransactionId()
{
	return m_id;
}

XSIPCall* XSIPTransaction::getOwner()
{
	return m_owner;
}

void XSIPTransaction::setOwner(XSIPCall* owner)
{
	m_owner = owner;
}

XSIPTransport* XSIPTransaction::getTransport()
{
	return m_transport;
}

void XSIPTransaction::setTransport(XSIPTransport* transport)
{
	m_transport = transport;
}

XSIPRequest* XSIPTransaction::getRequest()
{
	return m_request;
}

XSIPResponse* XSIPTransaction::getResponse()
{
	return m_response;
}

void XSIPTransaction::setRequest(XSIPRequest* request)
{
	m_request = request;
}

void XSIPTransaction::setResponse(XSIPResponse* response)
{
	m_response = response;
}

XSIPTransactionState* XSIPTransaction::getTranactionState()
{
	return m_state;
}

void XSIPTransaction::setTransactionState(XSIPTransactionState* state)
{
	m_state = state;
}

int XSIPTransaction::getState()
{
	return m_tsxState;
}

void XSIPTransaction::setState(int state)
{
	m_tsxState = state;
}

XSIPRequest* XSIPTransaction::getAck()
{
	return m_ack;
}

void XSIPTransaction::setAck(XSIPRequest* request)
{
	m_ack = request;
}